/**
 * 发货通知
 * @author miaoyl
 * @date 2018/03/07
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'TD049', width: 160, title: '订单单号', sort: true}
            , {
                field: 'MA002', width: 140, title: '客户', search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {field: 'TD048', width: 80, title: '订单项次', sort: true}
            , {
                field: 'TC002', width: 140, title: 'ERP订单单号'
                ,
                search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {field: 'TD003', width: 120, title: 'ERP订单项次', sort: true}
            , {
                field: 'TC003', width: 100, title: '订单日期'
                , search: 'default',
                searchOpts: {type: 'daterange'}, sort: true
            }
            , {
                field: 'TD004', width: 160, title: '品号'
                , search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {field: 'TD005', width: 160, title: '品名'}
            , {field: 'TD006', width: 160, title: '规格'}
            , {field: 'TD010', width: 80, title: '单位'}
            , {
                field: 'TD007', width: 100, title: '仓库'
                , search: 'default',
                searchOpts: {icon: true}, sort: true
            }
            , {
                field: 'TCI01', width: 120, title: '预交货日期'
                , search: 'default',
                searchOpts: {type: 'daterange'}, sort: true
            }
            , {field: 'TD008', width: 100, title: '订单数量', sort: true}
            , {field: 'TH008_SUM', width: 100, title: '已发货数量', sort: true}
            , {field: 'NUM', width: 100, title: '未发货数量', sort: true, edit: 'text'}
            , {field: 'ERP_NUM', width: 100, title: 'ERP库存数量', sort: true}
            , {field: 'NUM1', width: 100, title: 'ERP库存数量', hide: true}
        ]],
        moduleName = '发货通知',
        mlegal,
        mlegal_choice
    ;

    app.newModule({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai750',
                _searchs: [{
                    id: 'gwai750', tableId: 'gwai750_table', datas: _searchsArr.pushArray([
                        {
                            field: 'mlegal', title: '法人', search: 'default',
                            searchOpts: {icon: true}
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'common_open_kh', tpl: 'common/_open_kh'
                }, {
                    dom: 'common_open_cp', tpl: 'common/_open_cp'
                }, {
                    dom: 'common_open_gwde', tpl: 'common/_open_gwde'
                }, {
                    dom: 'common_open_coptc', tpl: 'common/_open_coptc'
                }, {
                    dom: 'common_open_ck', tpl: 'common/_open_ck'
                }
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai750_table'),
                    url: g_ctxPath + 'crm/gwai750/getListPage',
                    exportsUrl: g_ctxPath + 'crm/gwai750/getListPage',
                    exportsName: '发货通知数据',
                    title: '发货通知信息表',
                    id: 'gwai750_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where,
                });

                var tipsIndex = null;

                //监听事件
                table.on('toolbar(gwai750_table)', function (obj) {
                    switch (obj.event) {
                        case 'transform':
                            var checkStatus = table.checkStatus('gwai750_table');
                            var _xdata = [];
                            var _ydata = [];
                            var _xstr_data = [];
                            var flag = true;
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    var _xstr = "";
                                    var _xtemp = app.fields_("gwec_t");
                                    var _ytemp = app.fields_("gwec_t");
                                    if (v.NUM <= 0 || !(/^[0-9]*$/).test(v.NUM)) {
                                        layer.msg('数量不可小于等于0且为整数');
                                        flag = false;
                                    } else if (v.NUM > v.NUM1) {
                                        layer.msg('数量不可大于订单数量-已发货数量');
                                        flag = false;
                                    }
                                    _xstr = table_where.mlegal + '/' + v.TC004 + '/' + v.TC200;
                                    if (_xstr_data.indexOf(_xstr) == -1) {
                                        _xstr_data.push(_xstr);
                                        _xtemp.GWECMLEGAL = mlegal;//法人
                                        _xtemp.GWEC004 = v.TC004;//客户
                                        _xtemp.GWEC102 = v.TC200;//付款条件
                                        _xdata.push(_xtemp);
                                    }

                                    _ytemp.GWECMLEGAL = mlegal;
                                    _ytemp.GWEC004 = v.TC004;
                                    _ytemp.GWEC102 = v.TC200;
                                    _ytemp.GWEC003 = v.TC002;//erp订单号
                                    _ytemp.GWEC103 = v.TD003;//序号
                                    _ytemp.GWEC009 = v.NUM;
                                    _ydata.push(_ytemp);
                                });
                                if(flag) {
                                    app.confirm({
                                        msg: '是否确认抛转？',
                                        ok: function () {
                                            self.transform(_xdata, _ydata, mlegal);
                                        }
                                    });
                                }
                            } else {
                                app.showWarning('请先选择数据!');
                                return;
                            }
                            break;
                    }
                });

                table.on('edit(gwai750_table)', function (obj) {
                    // if (obj.field == 'NUM') {
                    //     if (obj.value <= 0 || !(/^[0-9]*$/).test(obj.value)) {
                    //         layer.msg('数量不可小于等于0且为整数');
                    //         obj.updateOfEdit({
                    //             'NUM': obj.data.NUM1
                    //         });
                    //     } else if (obj.value > obj.data.NUM1) {
                    //         layer.msg('数量不可大于订单数量-已发货数量');
                    //         obj.updateOfEdit({
                    //             'NUM': obj.data.NUM1
                    //         });
                    //     }
                    // }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai750_table', where: table_where});
        },

        transform: function (_xdata, _ydata, mlegal) {
            app.ajax({
                xurl: 'crm/gwai750/transform',
                xdata: {_xdata: _xdata, _ydata: _ydata, mlegal: mlegal},
                loading: true,
                toJsonStr: true,
                okmsg: moduleName + '抛转成功',
                cb: function (result) {
                    if (result.ok) {
                        app.showMessage("抛转成功,销货单号为：" + result.data.data);
                        self.reloadTableCurr();
                    }
                }
            });
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai750',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'MA002') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_kh',
                                    tagData: {
                                        cb: function (data) {
                                            vue.isearch.fields.MA002 = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '430px']
                                    }
                                });
                            } else if (field == 'TD004') {//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_cp',
                                    tagData: {
                                        cb: function (data) {
                                            vue.isearch.fields.TD004 = data.GWAM001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '430px']
                                    }
                                });
                            } else if (field == 'mlegal') {//法人开窗
                                self._openTplFull({
                                    tag: 'common_open_gwde',
                                    tagData: {
                                        GWDE002: 'ERP账套',
                                        cb: function (data) {
                                            vue.isearch.fields.mlegal = data.GWDE003;
                                            mlegal = data.GWDE003;
                                            mlegal_choice = data.GWDE003;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择法人',
                                        area: ['600px', '430px']
                                    }
                                });
                            } else if (field == 'TC002') {//订单开窗
                                if (vue.isearch.fields.mlegal == null || vue.isearch.fields.mlegal == '') {
                                    app.showMessage("请先选择法人！");
                                    return;
                                }
                                self._openTplFull({
                                    tag: 'common_open_coptc',
                                    tagData: {
                                        mlegal: mlegal,
                                        cb: function (data) {
                                            vue.isearch.fields.TC002 = data.TC002;
                                        }
                                    },
                                    index: 3,
                                    openOpts: {
                                        title: '选择订单',
                                        area: ['600px', '430px']
                                    }
                                });
                            } else if (field == 'TD007') {//仓库开窗
                                self._openTplFull({
                                    tag: 'common_open_ck',
                                    tagData: {
                                        cb: function (data) {
                                            vue.isearch.fields.TD007 = data.GWAS001;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择仓库',
                                        area: ['600px', '430px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.TC003': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.TC003_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.TC003_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.TC003_START = '';
                                this.isearch.fields.TC003_END = '';
                            }
                        },
                        'isearch.fields.TCI01': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.TCI01_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.TCI01_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.TCI01_START = '';
                                this.isearch.fields.TCI01_END = '';
                            }
                        },
                        'isearch.fields.mlegal': function () {
                            if (this.isearch.fields.mlegal == null || this.isearch.fields.mlegal == '') {
                                mlegal_choice = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
            $('#itoSearch').click(function () {
                if (mlegal_choice == null || mlegal_choice == '') {
                    app.showMessage("法人必选！");
                    return;
                }
            })
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});